package com.example.yunifang.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.example.yunifang.FenLeiActivity;
import com.example.yunifang.R;
import com.example.yunifang.XiangQingActivity;
import com.example.yunifang.adapter.FenleiFragmentAdapter;
import com.example.yunifang.bean.FenLeiChildrenBean;
import com.example.yunifang.bean.FenLeiChildrenBean.Data;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class FenLeiFragment extends Fragment {
	private GridView fenlei_lei_gd;
	private String id;
	private int pos;
	private ArrayList<Data> data = new ArrayList<Data>();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = View.inflate(getActivity(), R.layout.fenlei_fragment, null);
		init(view);
		getJson();
		return view;
	}

	public void init(View view) {
		fenlei_lei_gd = (GridView) view.findViewById(R.id.fenlei_fragment_gv);
		Bundle bundle = getArguments();
		pos = bundle.getInt("pos");
		id = FenLeiActivity.name.children.get(pos).id;
	}

	private void getJson() {
		// TODO Auto-generated method stub
		HttpUtils utils = new HttpUtils();
		utils.send(
				HttpMethod.GET,
				"http://m.yunifang.com/yunifang/mobile/goods/getall?random=68506&encode=21f7b9924f3c19bc280825a2b90f13c3&category_id="
						+ id, new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// TODO Auto-generated method stub
						String json = arg0.result;
						Gson gson = new Gson();
						final FenLeiChildrenBean bean = gson.fromJson(json,
								FenLeiChildrenBean.class);
						data.clear();
						data.addAll(bean.data);
						FenleiFragmentAdapter adapter = new FenleiFragmentAdapter(
								data, getActivity());
						fenlei_lei_gd.setAdapter(adapter);
						fenlei_lei_gd.setOnItemClickListener(new OnItemClickListener() {

							@Override
							public void onItemClick(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								// TODO Auto-generated method stub
								String id=bean.data.get(arg2).id;
								int ids=Integer.parseInt(id);
								Intent intent=new Intent(getActivity(),XiangQingActivity.class);
								intent.putExtra("id", ids);
								startActivity(intent);
								getActivity().overridePendingTransition(R.anim.outactivity, R.anim.inactivity);
							}
						});
					}
				});
	}

}
